{
 "cells": [
  {
   "cell_type": "code",
   "id": "9ae2de40-6aa6-4669-9360-0f5a90427f16",
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "source": [
    "def divide_by_input():\n",
    "    while True:\n",
    "        try:\n",
    "            # 提示用户输入\n",
    "            number = int(input(\"请输入一个整数：\"))\n",
    "            # 执行除法运算\n",
    "            result = 100 / number\n",
    "            print(f\"100除以{number}的商是：{result}\")\n",
    "            # 如果代码在没有异常的情况下运行，打印以下消息\n",
    "            print(\"程序正常运行，没有捕捉到异常\")\n",
    "            break\n",
    "        except ValueError:\n",
    "            # 如果输入的不是整数，再次提示用户\n",
    "            print(\"输入不正确，请输入一个整数。\")\n",
    "        except ZeroDivisionError:\n",
    "            # 如果输入的是零，再次提示用户\n",
    "            print(\"不能除以零，请重新输入一个非零整数。\")\n",
    "\n",
    "# 调用函数\n",
    "divide_by_input()\n"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "79106cfe-5615-4c75-9807-66185914106e",
   "metadata": {},
   "outputs": [],
   "source": [
    "chars = [\"a\", \"b\", 100, -37, 2]\n",
    "\n",
    "def access_element(index):\n",
    "    try:\n",
    "        # 尝试访问给定索引处的元素\n",
    "        element = chars[index]\n",
    "        return f\"元素是：{element}\"\n",
    "    except IndexError:\n",
    "        # 处理索引超出范围的情况\n",
    "        return \"发生索引超出范围异常（IndexError）\"\n",
    "\n",
    "# 示例用法：访问一个超出范围的索引\n",
    "result = access_element(10)  # 这将触发 IndexError\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "924283d1-36d0-4fea-b9bc-1c6f2395fac6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'发生索引超出范围异常（IndexError）'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Below is a Python code snippet that demonstrates an IndexError by attempting to access an index\n",
    "# that is out of range for the given list `chars`.\n",
    "\n",
    "chars = [\"a\", \"b\", 100, -37, 2]\n",
    "\n",
    "def access_element(index):\n",
    "    try:\n",
    "        # Attempt to access the element at the given index\n",
    "        element = chars[index]\n",
    "        return f\"元素是：{element}\"\n",
    "    except IndexError:\n",
    "        # Handle the case where the index is out of range\n",
    "        return \"发生索引超出范围异常（IndexError）\"\n",
    "\n",
    "# Example usage: Accessing an index that is out of range\n",
    "result = access_element(10)  # This will trigger the IndexError\n",
    "result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "720be9b7-668e-47db-a881-ac2d4c85f304",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d17f7dcf-a333-4154-92f5-f1af703fea4b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
